package com.sec.etech.employeesync.confighandler;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sec.etech.employeesync.entity.OrgEmployeeZSecondaryLevelJob;
import com.sec.etech.employeesync.mapper.OrgEmployeeZSecondaryLevelJobMapper;
import com.sec.etech.employeesync.util.ConfigTableHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
@Slf4j
public class SecondaryLevelJobConfigHandler implements ConfigTableHandler<OrgEmployeeZSecondaryLevelJob> {

    @Resource
    private OrgEmployeeZSecondaryLevelJobMapper secondaryLevelJobMapper;

    @Override
    public Long getOrCreateConfigId(String... params) {
        String code = params[0];
        String name = params[1];
        OrgEmployeeZSecondaryLevelJob secondaryLevelJob = getEntityByName(code, name);
        if (secondaryLevelJob == null) {
            secondaryLevelJob = new OrgEmployeeZSecondaryLevelJob();
            secondaryLevelJob.setCode(code);
            secondaryLevelJob.setName(name);
            secondaryLevelJob.setActive(1);
            insertEntity(secondaryLevelJob);
            log.info("新增二级岗位配置:name={}", name);
        }
        return secondaryLevelJob.getSecondaryLevelJobId();
    }

    @Override
    public OrgEmployeeZSecondaryLevelJob getEntityByName(String name) {
        return secondaryLevelJobMapper.selectOne(Wrappers.<OrgEmployeeZSecondaryLevelJob>lambdaQuery()
                .eq(OrgEmployeeZSecondaryLevelJob::getName, name));
    }

    public OrgEmployeeZSecondaryLevelJob getEntityByName(String code, String name) {
        return secondaryLevelJobMapper.selectOne(Wrappers.<OrgEmployeeZSecondaryLevelJob>lambdaQuery()
                .eq(OrgEmployeeZSecondaryLevelJob::getCode, code)
                .eq(OrgEmployeeZSecondaryLevelJob::getName, name));
    }

    @Override
    public String generateNextCode() {
        return null;
    }

    @Override
    public void insertEntity(OrgEmployeeZSecondaryLevelJob entity) {
        secondaryLevelJobMapper.insert(entity);
    }
}
